package gui.GLListeners.GLCirculos;

import figuras.Circulo;
import figuras.PuntoVector;

import java.awt.Color;
import java.awt.event.MouseEvent;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLJPanel;
import javax.swing.SwingUtilities;

import aplicacion.Vista;

public class CirculosLib extends Circulos{
		
	public CirculosLib(GLJPanel panel, Vista v){
		super(panel, v);
	}
	
	public void display(GLAutoDrawable drw) {
		
		super.display(drw);
		GL gl = drw.getGL();
		dibujaCirculos(gl);		
    }
	
	private void dibujaCirculos(GL gl){

		circulo1 = new Circulo(50, componentes.getRadio(0), 0,0,0);
		circulo2 = new Circulo(50, componentes.getRadio(1), 0,0,0);
		circulo3 = new Circulo(50, componentes.getRadio(2), 0,0,0);
		circulo4 = new Circulo(50, componentes.getRadio(3), 0,0,0);
		
		//dibujamos los circulos  
        gl.glEnable(GL.GL_LINE_STIPPLE);
        gl.glLineStipple(3, (short)0xCCCC);
        circulo1.dibuja(gl, new Color(0,0,0), GL.GL_LINE_LOOP);
        circulo2.dibuja(gl, new Color(0,0,0), GL.GL_LINE_LOOP);
        circulo3.dibuja(gl, new Color(0,0,0), GL.GL_LINE_LOOP);
        circulo4.dibuja(gl, new Color(0,0,0), GL.GL_LINE_LOOP);
        gl.glDisable(GL.GL_LINE_STIPPLE);     
	}
	
	public void mousePressed(MouseEvent e) {
		
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getClickCount() == 2){  
			
			if(SwingUtilities.isLeftMouseButton(e)){
				double x = (e.getX())
						* ((super.xRight - super.xLeft) / (double) lienzo
								.getWidth()) + super.xLeft - super.xCentro;
				double y = (-e.getY())
						* ((super.yTop - super.yBot) / (double) lienzo
								.getHeight()) + super.yTop - super.yCentro;

				PuntoVector mouse = new PuntoVector((float) x - 467, (float) y,
						0.0f, 1);
				vista.openLib(mouse);
			}
			else{
				double x = (e.getX())
					* ((super.xRight - super.xLeft) / (double) lienzo
						.getWidth()) + super.xLeft - super.xCentro;
				double y = (-e.getY())
					* ((super.yTop - super.yBot) / (double) lienzo.getHeight())
					+ super.yTop - super.yCentro;

				int c = dameCirculo(x, y);

				vista.openLib(c);
			}
		 }   		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		
	}


	private int dameCirculo(double x, double y) {
		double r = Integer.MAX_VALUE;
		
		//calculamos el radio
		PuntoVector pv = new PuntoVector((float) x-467, (float) y, 0.0f, 1);
		//r = pv.modulo();
		r = pv.distancia2D(new PuntoVector(0,0,0,1));
		
		if (r <= circulo4.getRadio())
			return 3;
		else if(r <= circulo3.getRadio())
			return 2;
		else if(r <= circulo2.getRadio())
			return 1;
		else if(r <= circulo1.getRadio())
			return 0;
		
		return -1;
	}

}
